<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Service\Common\AnalyzeXlsxService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;

class AnalyzeXlsxController extends BaseController
{
    /**
     * 获取文件内容
     */
    public function analyzeXlsx(Request $request): JsonResponse
    {
        $uploadedFile = $request->file('file');

        if (!$uploadedFile instanceof UploadedFile) {
            return $this->fail(400, '请上传文件');
        }

        // 限制文件大小 1MB
        if ($uploadedFile->getSize() > 1024 * 1024) {
            return $this->fail(400, '文件大小超过限制');
        }

        $allowed_extensions = ['csv', 'xlsx', 'Xlsx'];
        if (!in_array(strtolower($uploadedFile->getClientOriginalExtension()), $allowed_extensions)) {
            return $this->fail(400, '只能上传 ' . implode(',', $allowed_extensions) . ' 格式的文件.');
        }

        $msg = AnalyzeXlsxService::logic($uploadedFile, 'importSupplierCruiseStaff');

        return $this->fail(200, $msg, []);
    }


}